#!/bin/bash

topdir=$(dirname $(realpath $0))

HAASUI_FOUNDATION_DIR=${topdir}/foundation
HAASUI_APPLICATIONS_DIR=${topdir}/applications
HAASUI_MISC_DIR=${topdir}/misc
HAASUI_OUTPUT_DIR=${topdir}/output

HAASUI_TARGET_DEVICE_DIR="${HAASUI_APPLICATIONS_DIR}"
HAASUI_TARGET_DEVICE=""
HAASUI_TARGET_DEVICE_ARRAY=($(cd ${HAASUI_TARGET_DEVICE_DIR}/; ls | sort))
HAASUI_TARGET_DEVICE_ARRAY_LEN=${#HAASUI_TARGET_DEVICE_ARRAY[@]}

HAASUI_TARGET_VERSION=""

SHELL_COLOR_NORMAL="\e[0m"
SHELL_COLOR_TXT_RED="\e[1;31m"
SHELL_COLOR_TXT_GREEN="\e[1;32m"
SHELL_COLOR_TXT_BLUE="\e[1;34m"


# Fixme: 以下内容需要自己定義
#############################

#############################


function build_error()
{
    if [ $# -ne 0 ]
    then
        echo -e ${SHELL_COLOR_TXT_RED} $1 ${SHELL_COLOR_NORMAL}
        exit 1;
    fi
}

function build_info()
{
    if [ $# -ne 0 ]
    then
        echo -e ${SHELL_COLOR_TXT_BLUE} $1 ${SHELL_COLOR_NORMAL}
    fi
}

function build_debug()
{
    if [ $# -ne 0 ]
    then
        echo -e ${SHELL_COLOR_TXT_GREEN} $1 ${SHELL_COLOR_NORMAL}
    fi
}


function build_usage()
{
    echo ""
    echo "Usage: $0 <build-target>"
    echo ""
    build_debug "  Available build targets:"
    echo ${HAASUI_TARGET_DEVICE_ARRAY[@]} | xargs -n 1 | sed "=" | sed "N;s/\n/. /"
    echo ""
}


function build_check_device()
{
    build_debug "select device: ${HAASUI_TARGET_DEVICE}"
    if [ -z ${HAASUI_TARGET_DEVICE} ] || [ ! -e $HAASUI_TARGET_DEVICE_DIR/$HAASUI_TARGET_DEVICE ];then
        build_error "No such device: $HAASUI_TARGET_DEVICE_DIR/$HAASUI_TARGET_DEVICE"
        exit 1
    fi
}

function build_select_device()
{
    echo "HAASUI_TARGET_DEVICE_DIR = ${HAASUI_TARGET_DEVICE_DIR}"
    echo ""
    echo "You're building on Linux with haasui"
    build_info "pick a combo of device:"
    echo ""

    if [ $HAASUI_TARGET_DEVICE_ARRAY_LEN -eq 0 ]; then
            build_error "No available device"
            exit 1
    fi

    echo ${HAASUI_TARGET_DEVICE_ARRAY[@]} | xargs -n 1 | sed "=" | sed "N;s/\n/. /"

    local INDEX
    read -p "Which would you like? [1]: " INDEX
    INDEX=$((${INDEX:-1} - 1))

    if echo $INDEX | grep -vq [^0-9]; then
        HAASUI_TARGET_DEVICE="${HAASUI_TARGET_DEVICE_ARRAY[$INDEX]}"
    else
        build_error "Unkown device"
        exit 1
    fi
    build_check_device
    build_info "Building target device: ${HAASUI_TARGET_DEVICE}"
    echo ""
}


function build_check_version()
{
    build_debug "select version: ${HAASUI_TARGET_VERSION}"
    if [ -z ${HAASUI_TARGET_VERSION} ] || [ ! -e ${HAASUI_TARGET_DEVICE_DIR}/${HAASUI_TARGET_DEVICE}/${HAASUI_TARGET_VERSION} ];then
        build_error "No such version: ${HAASUI_TARGET_DEVICE_DIR}/${HAASUI_TARGET_DEVICE}/${HAASUI_TARGET_VERSION}"
        exit 1
    fi
}

function build_select_version()
{
    HAASUI_TARGET_VERSION_ARRAY=($(cd ${HAASUI_TARGET_DEVICE_DIR}/${HAASUI_TARGET_DEVICE}/; ls  | sort))
    HAASUI_TARGET_VERSION_ARRAY_LEN=${#HAASUI_TARGET_VERSION_ARRAY[@]}

    echo "HAASUI_TARGET_DEVICE = ${HAASUI_TARGET_DEVICE}"
    echo ""
    echo "You're building on Linux with ${HAASUI_TARGET_DEVICE}"
    echo "pick a combo version:"
    echo ""

    if [ $HAASUI_TARGET_VERSION_ARRAY_LEN -eq 0 ]; then
            echo "No available version"
            exit 1
    fi

    echo ${HAASUI_TARGET_VERSION_ARRAY[@]} | xargs -n 1 | sed "=" | sed "N;s/\n/. /"

    local INDEX
    read -p "Which would you like? [1]: " INDEX
    INDEX=$((${INDEX:-1} - 1))

    if echo $INDEX | grep -vq [^0-9]; then
            HAASUI_TARGET_VERSION="${HAASUI_TARGET_VERSION_ARRAY[$INDEX]}"
    else
            echo "Unkown version"
            exit 1
    fi
    build_check_version
    echo "Building target version: ${HAASUI_TARGET_VERSION}"
    echo ""
}

function build_check_args()
{
    if echo $@ | grep -wqE "help|-h"; then
        build_usage $*
        exit 0
    fi

    if [ $# -eq 0 ]; then
        HAASUI_TARGET_DEVICE=""
    elif [ $# -eq 1 ]; then
        HAASUI_TARGET_DEVICE=$1
    else
        build_usage $*
    fi
}



function build_prepare()
{
    mkdir -p ${HAASUI_OUTPUT_DIR}
}

function build_clean()
{
    rm -rf ${HAASUI_OUTPUT_DIR}/*
}

function build_tar2bin()
{
    if [ $# -ne 2 ]; then
        build_error "tar2bin Invalid args"
        exit 1
    fi
    TARGET_FILE_TAR=$1
    TARGET_FILE_BIN=$2
    BLANK_FILE=${HAASUI_MISC_DIR}/blank.bin
    HEAD_SIZE=64
    
    if [ ! -e ${TARGET_FILE_TAR} ] || [ ! -e ${BLANK_FILE} ]; then
        build_error "File not found"
        exit 1
    fi

    HEAD_SIZE=64
    TARGET_HASH_FILE="hash.txt"
    TARGET_HEAD_FILE="head.bin"
    sha256sum $TARGET_FILE_TAR | awk '{printf $1}' > $TARGET_HASH_FILE
    cp $BLANK_FILE $TARGET_HEAD_FILE
    dd if=$TARGET_HASH_FILE of=$TARGET_HEAD_FILE bs=$HEAD_SIZE  skip=0 seek=0 conv=notrunc
    cat $TARGET_HEAD_FILE $TARGET_FILE_TAR > $TARGET_FILE_BIN
}

function build_firmware()
{
    HAASUI_TARGET_FIRMWARE_TAR=walos-${HAASUI_TARGET_DEVICE}.${HAASUI_TARGET_VERSION}.firmware.tar.gz
    HAASUI_TARGET_FIRMWARE_BIN=walos-${HAASUI_TARGET_DEVICE}.${HAASUI_TARGET_VERSION}.firmware.bin
    HAASUI_TARGET_UPGRADE_TAR=walos-${HAASUI_TARGET_DEVICE}.${HAASUI_TARGET_VERSION}.upgrade.tar.gz
    HAASUI_TARGET_UPGRADE_BIN=walos-${HAASUI_TARGET_DEVICE}.${HAASUI_TARGET_VERSION}.upgrade.bin
    HAASUI_RESORCE_DIR="haasui/etc/miniapp/resources"
    HAASUI_RESORCE_PACKAGES_JSONFILE="local_packages.json"

    cp -r ${HAASUI_FOUNDATION_DIR}/* ${HAASUI_OUTPUT_DIR}/
    cp -r ${HAASUI_TARGET_DEVICE_DIR}/${HAASUI_TARGET_DEVICE}/${HAASUI_TARGET_VERSION} ${HAASUI_OUTPUT_DIR}/${HAASUI_RESORCE_DIR}
    
    pushd ${HAASUI_OUTPUT_DIR}/${HAASUI_RESORCE_DIR}
    ln -sf ${HAASUI_TARGET_VERSION}/${HAASUI_RESORCE_PACKAGES_JSONFILE}
    popd

    pushd ${HAASUI_OUTPUT_DIR}
    tar zcvf ${HAASUI_TARGET_FIRMWARE_TAR} *
    build_tar2bin ${HAASUI_TARGET_FIRMWARE_TAR} ${HAASUI_TARGET_FIRMWARE_BIN}
    popd

    pushd ${HAASUI_OUTPUT_DIR}/${HAASUI_RESORCE_DIR}
    tar zcvf ${HAASUI_TARGET_UPGRADE_TAR} ${HAASUI_TARGET_VERSION} ${HAASUI_RESORCE_PACKAGES_JSONFILE}
    build_tar2bin ${HAASUI_TARGET_UPGRADE_TAR} ${HAASUI_TARGET_UPGRADE_BIN}
    popd
}

function build_target()
{
    build_firmware
}

function build_main()
{
    build_check_args $*
    build_select_device
    build_select_version

    build_prepare
    build_clean
    build_target
}

if [ ! -d ${HAASUI_FOUNDATION_DIR} ] || [ ! -d ${HAASUI_APPLICATIONS_DIR} ]; then
    build_error "Invalid packet sdk"
    exit 1
fi

build_main $*
